package org.ocean.general.sample.helper;

import org.ocean.general.sample.page.PageInfo;

import java.util.Objects;

/**
 * @author lizhansheng
 * @Title
 * @Description
 * @date 2021/12/9
 * @return
 */
public class PageHelper {

    private static final ThreadLocal<PageInfo> threadLocal = new ThreadLocal<PageInfo>();

    public static  PageInfo get(){
        return threadLocal.get();
    }

    public static void set(PageInfo pageInfo){
        threadLocal.set(pageInfo);
    }

    public static void remove(){
        threadLocal.remove();
    }

    private static void startPage(PageInfo pageInfo){
        pageInfo.startPageInfo();
    }


    public static <T> PageInfo<T> doPage(PageInfo pageInfo, SelectHelper selectHelper){
        if(Objects.nonNull(pageInfo.getPage())){
            startPage(pageInfo);
            pageInfo.setList(selectHelper.doSelect());
            if(Objects.nonNull(get())){
                pageInfo.setTotalCount(get().getTotalCount());
                remove();
            }
            return pageInfo;
        }
        return new PageInfo<>();
    }
}
